package com.hxzy.vo;

import com.fasterxml.jackson.annotation.JsonIgnore;
import com.fasterxml.jackson.annotation.JsonInclude;
import com.hxzy.entity.MallAdminUser;
import lombok.Getter;
import lombok.Setter;
import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.core.authority.SimpleGrantedAuthority;
import org.springframework.security.core.userdetails.User;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.util.Assert;

import java.util.*;

/**
 * 后台用户登录成功的模型
 */
@Getter
@Setter
public class AdminLoginVO implements UserDetails{

    /**
     * 当前用户
     */
    private MallAdminUser mallAdminUser;

    /**
     * 令牌
     */
    private String uuid;

    /**
     * 令牌的开始时间
     */
    private Long createTime;

    /**
     * 令牌的结束时间
     */
    private Long endTime;

    /**
     * 所有角色字符串 SYS_ROLE(role_key的值)
     */
    private Set<String>  roleSet;

    /**
     * 所有菜单字符串 SYS_MENU(perms的值)
     */
    private Set<String>  permissionSet;

    /**
     *  json序列化的时候需要的
     */
    public AdminLoginVO() {
    }

    public AdminLoginVO(MallAdminUser mallAdminUser, Set<String> roleSet, Set<String> permissionSet) {
        this.mallAdminUser = mallAdminUser;
        this.roleSet = roleSet;
        this.permissionSet = permissionSet;
    }

    /**
     * 判断是不是超级管理员
     * @return
     */
    @JsonIgnore
    public boolean isSuperMan(){
        return (this.roleSet!=null &&  roleSet.contains("admin"));
    }

    @JsonIgnore
    @Override
    public Collection<? extends GrantedAuthority> getAuthorities() {
        Set<GrantedAuthority> hashSet=new HashSet<>();
        //springsecurity角色权限 设计，前缀叫做  ROLE_    开头  ,AuthorityAuthorizationManager这个类中设定的
        for(String role: roleSet){
           hashSet.add(  new SimpleGrantedAuthority("ROLE_"+role));
        }
        //菜单权限
        for (String perms: permissionSet){
            hashSet.add(  new SimpleGrantedAuthority(perms));
        }
        return hashSet;
    }



    @JsonIgnore
    @Override
    public String getPassword() {
        return this.mallAdminUser.getLoginPassword();
    }

    @JsonIgnore
    @Override
    public String getUsername() {
        return this.mallAdminUser.getLoginName();
    }

    @JsonIgnore
    @Override
    public boolean isAccountNonExpired() {
        return true;
    }

    @JsonIgnore
    @Override
    public boolean isAccountNonLocked() {
        return true;
    }

    @JsonIgnore
    @Override
    public boolean isCredentialsNonExpired() {
        return true;
    }

    @JsonIgnore
    @Override
    public boolean isEnabled() {
        return true;
    }
}
